<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 15:05
 */

namespace App\Http\Controllers\Api\UserFriends;

use App\Http\Controllers\ApiController;
use App\LogicService\Contracts\UserFriendsLogicContract;
use App\Transformers\UserFriendTransformer;
use App\User;
use Illuminate\Http\Request;
use RongCloud;
use App\Models\UserBase;

class UserFriendsController extends ApiController
{


    /**
     * @var \Illuminate\Http\Request
     */
    protected $request;

    protected $userFriends;

    protected $rong;

    /**
     * UserFriendsController constructor.
     * @param Request $request
     * @param UserFriendsLogicContract $userFriends
     *
     */
    public function __construct(Request $request, UserFriendsLogicContract $userFriends)
    {
        parent::__construct();

        $this->request = $request;
        $this->rong = new RongCloud(config('app.rongcloud_appKey'), config('app.rongcloud_appSecret'));
        $this->userFriends = $userFriends;
    }


    public function store()
    {
        $this->validate($this->request, [
            'f_user_account' => 'required|string',
        ]);
        $user = \Auth::user();
        //判断用户是否存在
        $f_user_account = $this->request->input('f_user_account');
        if($user->account == $f_user_account){
            return $this->setMessage('不能添加自己为好友')->setCode('400004')->response();
        }
        $friend = UserBase::where('account', $this->request->input('f_user_account'))->first();
        if ($friend) {
            //判断是否是好友
            $result = $this->userFriends->show($user->account, $f_user_account);

            if (!$result) {
                //添加好友
                $data = ['user_account' => $user->account, 'f_user_account' => $f_user_account];
                $result = $this->userFriends->store($data);
                //给好友添加我为好友
                $data_f = ['user_account' => $f_user_account, 'f_user_account' => $user->account];
                $result_f = $this->userFriends->store($data_f);

                if ($result && $result_f) {

                    //推送用户
                    $f_user = $result->user;

                    if ($f_user->type == 1) {
                        $user_account = 'patient_' . $f_user->account;
                    } else {
                        $user_account = 'doctor_' . $f_user->account;
                    }

                    $push_arr = [
                        "platform" => ["ios", "android"],
                        "audience" => ["userid" => [$user_account]],
                        "notification" => ["alert" => '医生:' . $user->name . '添加您为好友!']
                    ];

                    $this->rong->push()->broadcastPush(json_encode($push_arr));
                }
            }
        } else {
            return $this->setMessage('好友用户不存在')->setCode('400004')->response();
        }

        return $this->item($result, new UserFriendTransformer());
    }


    public function delete()
    {

        $this->validate($this->request, [
            'f_user_account' => 'required|string',
        ]);


        $user = \Auth::user();
        //判断是否是好友

        $f_user_account = $this->request->input('f_user_account');
        $result = $this->userFriends->show($user->account, $f_user_account);
        $result_f = $this->userFriends->show($f_user_account, $user->account);
        $res = 0;
        if ($result) {
            $res = $result->delete();
        }
        $res_f = 0;
        if ($result_f) {
            $res_f = $result_f->delete();
        }

        if ($res && $res_f) {
            return $this->setMessage('删除好友成功')->setCode('10000')->response();
        }

        return $this->setMessage('删除好友失败')->setCode('400004')->response();
    }

    /**
     * 获取用户好友列表
     * user_type 1:患者 2:医生
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        $this->validate($this->request, [
            'user_type' => 'required|integer',
        ]);

        $user = \Auth::user();
        $user_type = $this->request->input('user_type');

        $data = $this->userFriends->getList($user->account,$user_type);


        return $this->collection($data, new UserFriendTransformer());
    }


    public function isFriend()
    {
        $this->validate($this->request, [
            'f_user_account' => 'required|string',
        ]);
        $user = \Auth::user();

        $friend = $this->userFriends->show($user->account, $this->request->input('f_user_account'));
        $result = 0;
        if ($friend) {
            $result = 1;
        }
        return $this->response(['is_friend' => $result]);
    }


    public function update()
    {
        $this->validate($this->request, [
            'f_user_account' => 'required|string',
            'memo_name' => 'required|string',
        ]);
        $user = \Auth::user();

        $f_user_account = $this->request->input('f_user_account');
        $memo_name = $this->request->input('memo_name');
        $friend = $this->userFriends->show($user->account, $f_user_account);

        if ($friend) {
            $friend->memo_name = $memo_name;
            $friend->save();
        } else {
            return $this->setMessage('修改备注名失败')->setCode('400004')->response();
        }
        return $this->item($friend, new UserFriendTransformer());
    }


    //搜索医生列表

    public function searchDoctors()
    {
        $this->validate($this->request, [
            'key' => 'required|string',
        ]);

        $key = $this->request->input('key');
        $user = \Auth::user();

        $doctors = User::select(['id', 'account', 'name', 'hospital_id'])
            ->where('name', $key)
            ->orwhere('phone', $key)
            ->get();

        foreach ($doctors as $key => &$doctor) {

            $friend = $this->userFriends->show($user->account, $doctor->account);
            $doctor->is_friend = 0;
            if ($friend) {
                $doctor->is_friend = 1;
            }

            if (!$doctor->detail) {
                $doctor->detail = array();
            } else {
                $doctor->detail->PIC = 'http://www.365tang.cn/upload/face/' . $doctor->detail->PIC;
            }
            if (!$doctor->detail) {
                $doctor->hospital = array();
            } else {
                $doctor->hospital;
            }

        }
        return $this->response($doctors->toArray());
    }
}